package com.study.ming.studydemo.thd;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {
    public static void main(String[] args) throws InterruptedException {
//        /*
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Runnable() {
            @Override
            public void run() {
//            2、这是三个人都到齐之后会执行的代码
                System.out.println(Thread.currentThread().getName() + "三个人都已到达会议室, 开始开会...");
            }
        });

//3、定义三个线程，相当于三个参会的人
        for (int i = 0; i < 3; i++) {
            final int finalI = i;
            new Thread() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "接到通知,开始出发....");
                    try {
                        //                    4、模拟每人到会议室所需时间
                        Thread.sleep((long) (Math.random() * 5000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("第" + Thread.currentThread().getName() + "个人到达会议室, 等待其他人到达");
                    try {
                        //                    5、等待其他人到会议室
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " 开始各抒己见,认真做笔记中...");
                }
            }.start();
        }
//        */

    }
}
